<?php

Class Nodelib extends Model {
    
    function Nodelib() {
        
        parent::Model();
        
        NodeORM::$db = $this->db;
    }
    
    
    public function view($file, $node) {
        
        $template = Template::$_global_template;
        
        if (file_exists(PATH_TO_TEMPLATE.$template.'modules/node/'.$node.'/'.$file.'.php')) {
            
            return $node.'/'.$file;
        }
        else {
            
            return $file;
        }
    }
}


Class NodeORM {
    
    static $db;
    private $node;
    
    
    public function __construct($node) {
        
        $this->node = $node;
    }
    
    
    public function get(NodeFilter $filter) {
        
        self::$db->from('node_'.$this->node);
        self::$db->where($filter->where);
        
        if ($filter->order_by != '') {
        
            self::$db->order_by($filter->order_by, $filter->order_type);
        }
        
        self::$db->limit($filter->limit, $filter->offset);
        
        return self::$db->get();
    }
    
    
    public function count(NodeFilter $filter) {
        
        self::$db->from('node_'.$this->node);
        self::$db->where($filter->where);
        
        return self::$db->count_all_results();
    }
    
    
    public function add(NodeValidate $data) {
        
        self::$db->insert('node_'.$this->node, $data->get());
    }
    
    
    public function update(NodeFilter $filter, NodeValidate $data) {
        
        self::$db->where($filter->where);
        self::$db->update('node_'.$this->node, $data->get()); 
    }
    
    
    public function delete(NodeFilter $filter) {
        
        self::$db->where($filter->where);
        self::$db->delete('node_'.$this->node);
        
        return true;
    }
    
    
    public function get_meta() {
                
        return self::$db->get('node_'.$this->node.'_meta')->result_array();
    }
}


Class NodeMeta {
    
    private $meta = array();
    
    public function __construct(array $meta) {
        
        $this->meta = $meta;
    }
    
    
    public function get_rules() {
        
        return $this->meta;
    }
    
    
    public function get_fields() {
        
        return $this->meta;
    }
}


Class NodeValidate {
    
    private $node;
    private $meta = array();
    private $post = array();
    
    public function __construct(NodeMeta $meta, $node) {
        
        $this->meta = $meta;
        $this->node = $node;
    }
    
    
    public function rules() {
        
        $return = array();
        
        foreach($this->meta->get_rules() as $row) {
            
            $return[] = array(
                'field' => $this->node.'_'.$row['name'],
                'rules' => 'trim|required|xss_clean',
                'label' => $row['title']
            );
        }
        
        return $return;
    }
    
    
    public function get() {
        
        return $this->post;
    }
    
    
    public function set(CI_Form_validation $valide) {
        
        foreach($this->meta->get_rules() as $row) {
            
            $name = $this->node.'_'.$row['name'];
            $this->post[$name] = $valide->set_value($name);
        }
        
        return $this;
    }
}


Class NodeFields {
    
    private $node;
    private $meta = array();
    private $form = array();
    
    public function __construct(NodeMeta $meta, $node) {
        
        $this->meta = $meta;
        $this->node = $node;
    }
    
    
    public function form_input($name, $value) {
        
        return form_input($name, $value);
    }
    
    
    public function form_textarea($name, $value) {
        
        return form_textarea($name, $value);
    }
    
    
    public function get($data = array()) {
        
        $result = array();
        
        foreach($this->meta->get_fields() as $row) {
            
            $method = 'form_'.$row['type'];
            $name = $this->node.'_'.$row['name'];
            $value = (isset($data[$name])) ? $data[$name] : '';
            
            if (!method_exists($this, $method)) continue;
            
            $result[$name] = array(
                'label' => $row['title'],
                'form'  => $this->$method($name, $value)
            );
        }
        
        return $result;
    }
}


Class NodeFilter {
    
    public $order_by = '';
    public $order_type = '';
    public $where = array();
    public $limit = 10;
    public $offset = 0;
    
    public function order($order_by, $order_type = 'DESC') {
        
        $this->order_by = $order_by;
        $this->order_type = $order_type;
        
        return $this;
    }
    
    
    public function where($name, $value) {
        
        $this->where[$name] = $value;
        
        return $this;
    }
    
    
    public function limit($limit, $offset = 0) {
        
        $this->limit = $limit;
        $this->offset = $offset;
        
        return $this;
    }
}